<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('关联用户')" />
    <script>
        if (!'[[${openId}]]' || !'[[${userId}]]') {
            window.location.href = '[[${contextPath}]]/wechat/login'
        }
    </script>
</head>
<body>
<div class="ibox float-e-margins">
    <div class="ibox-title">
        <h5>用户认证</h5>
    </div>
    <div class="ibox-content">
        <form class="form-horizontal m-t" id="commentForm">
            <input type="hidden" th:value="${openId}" id="openId">
            <input type="hidden" th:value="${userId}" id="userId">

            <div class="form-group">
                <label class="col-sm-3 control-label is-required">姓名：</label>
                <div class="col-sm-8">
                    <input id="psnName" name="name" minlength="2" type="text" class="form-control" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">手机号：</label>
                <div class="col-sm-8">
                    <input id="tel" type="text" class="form-control" name="tel" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">收货地址：</label>
                <div class="col-sm-8">
                    <input id="rec_addr" type="text" class="form-control" name="rec_addr">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-4 col-sm-offset-3">
                    <a class="btn btn-primary" type="submit" id="btn_submit">提交</a>
                </div>
            </div>
        </form>
    </div>
</div>
<th:block th:include="include :: footer" />
<script type="text/javascript">
    $(function(){
        $("#commentForm").validate();
        $("#btn_submit").click(function () {
            if ($("#psnName").val() && $("#tel").val()) {
                $.ajax({
                    cache : true,
                    type : "POST",
                    url : ctx + "wechat/addBind",
                    data : {
                        "psnName": $("#psnName").val(),
                        "tel": $("#tel").val(),
                        "recAddr": $("#rec_addr").val(),
                        "openId": $("#openId").val(),
                        "userId": $("#userId").val()
                    },
                    async : false,
                    error : function(request) {

                    },
                    success : function(data) {
                        if (data && data.code && data.code == 500) {
                            alert(data.msg)
                        } else {
                            window.location.href = '[[${contextPath}]]/wechat/login'
                        }
                    }
                });
            }
        })
    });
</script>
</body>
</html>
